import axios from "axios";
import store from '../store'

import {
    Notification
} from "element-ui";
import router from "../router";
import {
    SA_TOKEN_NAME,
    TOKEN_KEY
} from "../constant/system"


const NOTIFICATION_DURATION_TIME = 3000; // 提示消息默认延迟时间
axios.defaults.baseURL = "/api"; // 请求前缀
// axios.defaults.baseURL = "/api-test"; // 请求前缀

const request = axios.create({
    timeout: 120000, // 超时时间2分钟
});

//请求拦截器
request.interceptors.request.use((config) => {
    if (window.sessionStorage.getItem(TOKEN_KEY)) {
        config.headers[SA_TOKEN_NAME] = window.sessionStorage.getItem(TOKEN_KEY);
    }
    return config;
});

//响应拦截器
request.interceptors.response.use(
    function (response) {
        console.log("请求响应：", response)
        // 浏览器状态码
        let resStatus = response.status
        // 自定义状态码
        let resCode = response.data.code

        // 参数错误
        if (resStatus === 200 && resCode === 400) {
            Notification.error({
                message: response.data.message,
                duration: NOTIFICATION_DURATION_TIME,
            });
            return Promise.reject(response.data)
        }
		// 未登录
		else if (resStatus === 200 && resCode === 401){
			const msg = response.data.message
			Notification.error({
				message: msg,
				duration: NOTIFICATION_DURATION_TIME,
			})
			store.commit('RESET_STATE');
			setTimeout(() => {
			    router.replace("/login")
			}, 700);
		}
        // 权限不足
        else if (resStatus === 200 && resCode === 403) {
			const msg = response.data.message
			Notification.error({
			    message: msg,
			    duration: NOTIFICATION_DURATION_TIME,
			});
            // if (msg.includes('未提供有效')) {
            //     Notification.error({
            //         message: "登录已过期，请重新登录",
            //         duration: NOTIFICATION_DURATION_TIME,
            //     });
            // } else {

            // }
            // setTimeout(() => {
            //     router.replace("/login")
            // }, 700);
            return;
        }
        // else if()
        // 服务器内部错误
        else if (resStatus === 200 && resCode === 500) {
            Notification.error({
                message: "服务器内部错误",
                duration: NOTIFICATION_DURATION_TIME,
            });
            return Promise.reject(response.data)
        } else {
            // if (resCode !== 200) {
            //     Notification.error({
            //         message: "未知错误，请联系管理员",
            //         duration: NOTIFICATION_DURATION_TIME,
            //     });
            //     return Promise.reject(response.data)
            // }
        }

        return Promise.resolve(response.data);
    },
    function (error) {
        console.log("响应失败：", error.response)
        let respStatus = error.response.status;
        if (respStatus && respStatus === 500) {
            Notification.error({
                message: "服务器内部错误，请联系管理员",
                duration: NOTIFICATION_DURATION_TIME,
            });
        }
        return Promise.reject(error);
    }
);

export default request;

// let base="http://localhost:8888"

// export const doPost = (url, params) => {
//     return axios({
//         method: 'post',
//         url: `${base}${url}`,
//         data: params
//     })
// }

// export const doGet = (url, params) => {
//     return axios({
//         method: 'get',
//         url: `${base}${url}`,
//         data: params
//     })
// }
